package com.ruoyi.business.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.business.domain.dto.admin.user.AdjustUserPointsDTO;
import com.ruoyi.business.domain.dto.admin.user.UserKocSetDTO;
import com.ruoyi.business.domain.koc.TbUser;
import com.ruoyi.business.domain.vo.TbUserImportVO;
import com.ruoyi.business.domain.vo.business.TbUserDetailVO;
import com.ruoyi.business.domain.vo.wecom.admin.TbWecomCustomerChatGroupVO;
import com.ruoyi.business.listener.UserImportListener;
import com.ruoyi.business.service.koc.ITbUserService;
import com.ruoyi.business.service.wecom.ITbWecomChatGroupMemberService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.excel.ExcelResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 小程序用户
 *
 * @author chenran
 */
@Validated
@Api(value = "koc小程序用户", tags = {"koc小程序用户管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/user")
public class TbUserController extends BaseController {

    @Resource
    private ITbUserService tbUserService;

    @Resource
    private ITbWecomChatGroupMemberService tbWecomChatGroupMemberService;


    @ApiOperation("分页查询小程序用户列表")
    @SaCheckPermission("business:koc:user:page")
    @GetMapping("/list")
    public TableDataInfo<TbUser> list(TbUser user, PageQuery pageQuery) {
        return tbUserService.selectPageList(user, pageQuery);
    }


    @ApiOperation("根据id查询用户详情")
    @GetMapping("/get-user-by-id")
    public R<TbUserDetailVO> getUserDetailById(@ApiParam(name = "userId", required = true) @RequestParam(required = true) Long userId) {
        return R.ok(tbUserService.getUserDetailById(userId));
    }

    @ApiOperation("下载导入模板")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "小程序用户数据", TbUserImportVO.class, response);
    }


    @ApiOperation("小程序用户导入")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "file", value = "导入文件", dataType = "java.io.File", required = true),
    })
    @Log(title = "小程序用户导入", businessType = BusinessType.IMPORT)
    @SaCheckPermission("business:koc:user:import")
    @PostMapping("/importData")
    public R<Void> importData(@RequestPart("file") MultipartFile file) throws Exception {
        ExcelResult<TbUserImportVO> result = ExcelUtil.importExcel(file.getInputStream(), TbUserImportVO.class, new UserImportListener(false));
        return R.ok(result.getAnalysis());
    }

    @ApiOperation("设置小程序用户等级入口 设置为koc1级或者小红书koc2级")
    @SaCheckPermission("business:koc:user:kocLevelSet")
    @PutMapping("/set-koc-level")
    public R<Void> setUserKocLevel(@Validated @RequestBody UserKocSetDTO dto) {
        return toAjax(tbUserService.setUserKocLevel(dto));
    }

    @ApiOperation("调整用户积分")
    @SaCheckPermission("business:koc:user:kocLevelSet")
    @PostMapping("/adjust—user-points")
    public R<Void> adjustUserPoints(@Validated @RequestBody AdjustUserPointsDTO dto) {
        return toAjax(tbUserService.adjustUserPoints(dto));
    }

   /* *//**
     * 查询客户所在群聊列表信息
     *//*
    @ApiOperation("查询客户所在群聊列表信息")
    @GetMapping("/member-group-list")
    public R<List<TbWecomCustomerChatGroupVO>> memberGroupList(
        @ApiParam("客户类型 1表示该外部联系人是微信用户，2表示该外部联系人是企业微信用户 type传1时unionId必传 type传2时externalUserId必传 type传3时userId必传") @RequestParam(required = true) Integer type,
        @ApiParam("企微用户外部联系Id") @RequestParam(required = false) String externalUserId,
        @ApiParam("微信客户unionId") @RequestParam(required = false) String unionId,
        @ApiParam("系统用户id") @RequestParam(required = false) Long userId) {
        return R.ok(tbWecomChatGroupMemberService.memberGroupList(type, unionId, externalUserId, userId));
    }*/


}
